/**
 * 
 */
package dao;

import java.util.ArrayList;
import java.util.List;

import model.Categoria;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author Gonze
 * 
 */
public class CategoriaDAO {

	protected String tableName = "Categoria";

	private SQLiteDatabase database = null;

	public CategoriaDAO(Context ctx) {
		database = ctx.openOrCreateDatabase("gigo_financeiro",
				Context.MODE_PRIVATE, null);
	}

	public long insert(Categoria object) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("titulo", object.getTitulo());
		contentValues.put("descricao", object.getDescricao());
		contentValues.put("cor", object.getCor().toString());
		return database.insert(tableName, null, contentValues);
	}

	public void update(Categoria object) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("titulo", object.getTitulo());
		contentValues.put("descricao", object.getDescricao());
		contentValues.put("cor", object.getCor().toString());
		database.update(tableName, contentValues, "where idcategoria = ?",
				new String[] { String.valueOf(object.getIdCategoria()) });
	}

	public void delete(Categoria object) {
		database.delete(tableName, "where idcategoria = ?",
				new String[] { String.valueOf(object.getIdCategoria()) });
	}

	public List<Categoria> getList() {
		List<Categoria> lista = new ArrayList<Categoria>();
		Cursor cursor = null;
		try {
			cursor = database.query(tableName, new String[] { "idcategoria",
					"titulo", "descricao", "cor" }, null, null, null, null,
					null);
			if (cursor.getCount() < 0) {
				return lista;
			}
			for (int i = 0; i < cursor.getCount(); i++) {
				cursor.moveToPosition(i);
				Categoria categoria = new Categoria();
				categoria.setIdCategoria(cursor.getInt(0));
				categoria.setTitulo(cursor.getString(1));
				categoria.setDescricao(cursor.getString(2));
				categoria.setCor(cursor.getInt(3));
				lista.add(categoria);
			}
			return lista;
		} catch (SQLException e) {
			Log.e(this.getClass().getName() + "_getList", e.toString());
			return null;
		} catch (Exception e) {
			Log.e(this.getClass().getName() + "_getList", e.toString());
			return null;
		}
	}

	public Categoria getCategoria(int idCategoria) {
		Categoria categoria = new Categoria();
		Cursor cursor = null;
		try {
			cursor = database.query(tableName, new String[] { "idcategoria",
					"titulo", "descricao", "cor" }, "idcategoria = ?",
					new String[] { String.valueOf(idCategoria) }, null, null,
					null);
			if (cursor.getCount() < 0) {
				return null;
			}
			cursor.moveToFirst();
			categoria.setIdCategoria(cursor.getInt(0));
			categoria.setTitulo(cursor.getString(1));
			categoria.setDescricao(cursor.getString(2));
			categoria.setCor(cursor.getInt(3));
			return categoria;
		} catch (SQLException e) {
			Log.e(this.getClass().getName() + "_getList", e.toString());
			return null;
		} catch (Exception e) {
			Log.e(this.getClass().getName() + "_getList", e.toString());
			return null;
		}
	}

}
